﻿using OnlineOpinionAnalysis.Server.Data;

namespace OnlineOpinionAnalysis.Server.HostService
{
	public class SpiderHostService : IHostedService, IDisposable
    {
		private ILogger<SpiderHostService> _logger;
		private readonly IServiceScopeFactory _scopeFactory;
		private Timer _timer;

        public readonly List<int> Rids = new() {36,1,3,4,188,160,119};

        public SpiderHostService(ILogger<SpiderHostService> logger,IServiceScopeFactory scopeFactory)
		{
			_logger = logger;
			_scopeFactory= scopeFactory;
		}

		public Task StartAsync(CancellationToken cancellationToken)
		{
            _logger.LogInformation($"[{DateTime.Now}]爬取服务启动");
            // 定时启动后每60分钟更新一次数据
            _timer = new Timer(SaveBiliBiliVideoInfo, null, TimeSpan.Zero, TimeSpan.FromMinutes(60));
            return Task.CompletedTask;
		}

		public Task StopAsync(CancellationToken cancellationToken)
		{
			_logger.LogInformation($"[{DateTime.Now}]爬取服务终止");
			return Task.CompletedTask;
		}
		public void Dispose()
		{
			_timer.Dispose();
		}

        public async void SaveBiliBiliVideoInfo(object? state)
        {
            // 创建数据库上下文的作用域
            using var scope = _scopeFactory.CreateScope();
            var dbContext = scope.ServiceProvider.GetRequiredService<DataContext>();
            _logger.LogInformation($"[{DateTime.Now}]开始爬取bilibli各区热门数据");
			List<BiliVideo> videoData;
            // 遍历分区rid，爬取数据
            foreach (var item in Rids)
            {
				videoData = await SpiderHelper.GetBiliBiliVideoOrder(_logger, item);
                // 若视频已经被爬取过，则更新其信息
                foreach (var video in videoData)
                {
                    var oldVideo =await dbContext.BiliVideos.FindAsync(video.AvId);
                    if (oldVideo != null)
                    {
                        _logger.LogInformation("一个视频有了更新的信息，替换");
                        oldVideo = video;
                    }
                    else
                    {
                      await  dbContext.BiliVideos.AddAsync(video);
                    }
                }
                await Task.Delay(1000);
                // 保存更改
                await dbContext.SaveChangesAsync();
                _logger.LogInformation($"[{DateTime.Now}]爬取tid[{item}]数据完成");
			}
        }



    }
}
